var datatable = {};
var itemIndex = gindex;
var tmp = '';
var seldept = null;
var dua = $("#cdate");

$(function(){
	$("#cdate").datepicker({
		"dateFormat" : "yy-mm-dd"
	});
	$("#cdate").datepicker();
	$("#dpkor").datepicker();
});

function openforadd(){
	window.showModalDialog("addplanitem",window);
}

function addItem(code,name,unit,value,remark) {	
	var dataBody = $("#planBody");
	itemIndex++;
	var namePrefix = "bean.dataItems[" + (itemIndex) + "].owner.";
	var datas = [ {
		"type" : "text",
		"attrs" : {
			"name" : namePrefix + "code",
			"value" : code
		},
		"className":"code-field"
	}, {
		"type" : "text",
		"attrs" : {
			"name" : namePrefix + "name",
			"readOnly" : true,
			"value":name
		},
		"className":"code-field"
	}, {
		"type" : "text",
		"attrs" : {
			"name" : namePrefix + "priceUnit",
			"readOnly" : true,
			"value":unit
		},
		"className" : "price-unit"
	}, {
		"type" : "text",
		"attrs" : {
			"name" : "bean.dataItems[" + itemIndex + "].value",
			"value":value
		},
		"events" : {
			"blur" : function() {
				return judgeNumber(this);
			}
		}
		,
		"className" : "number-field"
	}, {
		"type" : "label",
		"attrs" : {
			"text" : '0'
		}
	}, {
		"type" : "text",
		"attrs" : {
			"name" : "bean.dataItems[" + itemIndex + "].modifiedValue",
			"value" : 0,
			"readOnly" : true
		},
		"events" : {
			"blur" : function() {
				return judgeNumber(this);
			}
		}
		,
		"className" : "number-field"
	},

	{
		"type" : "text",
		"attrs" : {
			"name" : "bean.dataItems[" + itemIndex + "].remark",
			"value":remark
		}
	,
	"className" : "code-field"
	}, {
		"type" : "img",
		"attrs" : {
			"src" : sbase + "/images/removeme.gif"
		},
		"csses" : {
			"cursor" : "hand"
		},
		"events" : {
			"click" : function() {
				$(this).parent().parent().remove();
			}
		}
	} ];
	addDataItem(datas, dataBody);
}

/**
 * 为该表格容器添加表格行
 * 
 * @param datas
 *            数据对象数组,包括三个域 type,events,attrs
 * @param container
 *            容器编号
 * 
 */
function addDataItem(datas, container) {
	var contar = container;
	var tr = $("<tr></tr>");
	contar.append(tr);
	$.each(datas, function(i, data) {
		var e = elementByType(data.type);
		e.attr(data.attrs || {});
		e.css(data.csses || {});
		e.bind(data.events || {});
		if (e.attr("autocmp") != undefined) {
			setAutoCmp(e);
		}
		if (data.type == 'hidden') {
			tr.append(e);
			return;
		}
		var td = $("<td></td>");
		td.append(e);
		tr.append(td);
	});
}


function saveplain() {
	var f = $("form#planform");
	var par = f.serialize();
	var url = base + "monthplanning/month-planning!" + actionOpr;
	$.post(url, par, function(data) {
		$("#messages").empty();

		// $("#messages").append(data);
	});

}

function download(id){
	var url= base + "monthplanning/month-planning!download?bean.id="+id;
	window.open(url,"_blank");
}
function closeMe() {
	if (confirm("确认关闭此页面?\n如果选 '确定':未保存的数据都将丢失,'取消':留在此页面继续~!")) {
		window.opener = null;
		window.close();
	}
}